package com.exercise.animations.reveal_animation

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewAnimationUtils
import androidx.core.animation.addListener
import com.exercise.animations.databinding.ActivityRevealAnimationBinding
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import kotlin.math.hypot

class RevealAnimationActivity : BaseActivity<BaseViewModel, ActivityRevealAnimationBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityRevealAnimationBinding
        get() = ActivityRevealAnimationBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        binding.btnVisible.setOnClickListener {
            visible()
        }
        binding.btnInvisible.setOnClickListener {
            invisible()
        }
    }

    private fun visible() {
        val imageView = binding.ivImage

        val cx = imageView.width / 2
        val cy = imageView.height / 2
        val finalRadius = hypot(cx.toDouble(), cy.toDouble()).toFloat()
        val anim = ViewAnimationUtils.createCircularReveal(imageView, cx, cy, 0f, finalRadius)
        imageView.visibility = View.VISIBLE
        anim.start()
    }

    private fun invisible() {
        val imageView = binding.ivImage

        val cx = imageView.width / 2
        val cy = imageView.height / 2
        val finalRadius = hypot(cx.toDouble(), cy.toDouble()).toFloat()
        val anim = ViewAnimationUtils.createCircularReveal(imageView, cx, cy, finalRadius, 0f)
        anim.addListener(
            onEnd = {
                imageView.visibility = View.INVISIBLE
            }
        )
        anim.start()
    }

    companion object {
        fun start(context: Context) {
            Intent(context, RevealAnimationActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}